package logicals

import (
	"io"
	"math/rand"
	"mime/multipart"
	"os"
	"path"
	"strconv"
	"time"
)

func UploadImage(image multipart.File, h *multipart.FileHeader) (int, string) {
	ext := path.Ext(h.Filename)
	defer image.Close()

	fileName := time.Now().Format("20060102150405")+strconv.Itoa(rand.Intn(1000)) + ext
	filePath := "F:\\Go\\GoProjects\\nginx-1.18.0\\images\\upload_images\\"
	path := filePath  + fileName

	targetFile, _ := os.OpenFile(path, os.O_WRONLY|os.O_CREATE|os.O_TRUNC, 0666)
	defer targetFile.Close()

	io.Copy(targetFile, image)

	return 200 ,"http://127.0.0.1/upload_images/"+fileName

}